package com.blindBox.back.controller.loginController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.domain.Manager;
import com.blindBox.back.service.ManagerService;
import com.blindBox.back.utils.JsonUtils;
import com.blindBox.back.utils.JwtUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/login")
public class LoginController
{
  @Autowired
  private ManagerService managerService;

  private final ObjectMapper objectMapper=new ObjectMapper();

  @PostMapping("/account_login")
  public MsgBackToFront verify_login(@RequestParam("name")String name, @RequestParam("pwd")String pwd)
  {
      Manager manager = managerService.getOne(new QueryWrapper<Manager>().eq("m_name", name).eq("m_pwd", pwd));
        //管理员账号或者密码输入错误
      if(manager==null)
        {
           return new MsgBackToFront(null,MsgBackToFront.MANAGER_LOGIN_ERROR);
        }
        //生成token
      String token = JwtUtils.createToken(manager.getId(), manager.getMName(), manager.getMPwd());
      return new MsgBackToFront(token,MsgBackToFront.HANDLE_SUCCESS);
  }
}
